import { MbUserToken } from './ml/mb_user_token';
import { failed } from './../../lib/utils';
import { OrderController } from './ctrl/order';
import { V, Connection, ExpressContext } from 'x-engine';
import { MainProxy, MainProxy2 } from './ctrl/index';
import { ProxyAPI } from './ctrl/api';
import { GetCache } from '../../lib/utils';
import { X } from 'x-orm';
import { ProxyUser } from './ml/proxy_user';
import { ProxyPageController } from './ctrl/page';
import { ProxyAuthAPI } from './ctrl/proxy';
import { ProxyMember } from './ml/proxy_member';
import { DownloadService } from './ctrl/download';
import { FrontApi } from './ctrl/front_api';
import { InviteController } from './ctrl/invite';

//注册控制器
ProxyAPI
ProxyAuthAPI;
MainProxy;
MainProxy2;
ProxyPageController;
OrderController;
DownloadService;
FrontApi;
InviteController;

ProxyMember;
ProxyUser;

export function common(){
    console.log(123231)
    console.log(arguments); 
}

//授权
V.registerAuthorization(Connection.HTTP,{
    async proxy(ctx : ExpressContext){
        const session : any = ctx.req.session;
        if(session.proxy_token){
            const id = await GetCache("proxy:token:" + session.proxy_token);
            if(id){
                const proxy = await X.of(ProxyUser).findOne({
                    where : {
                        id : id
                    }
                });
                if(proxy){
                    (ctx as any).proxyUser = proxy;
                    return true;
                }
            }
        }
        ctx.res.redirect('/proxy/login.html');
        return false; 
    },

    async member(ctx : ExpressContext){
        const token = ctx.req.body.token || ctx.req.query.token;
        if(token){
            const tinfo = await X.of(MbUserToken).findOne({
                where : {
                    token : token
                },
                addon : {
                    member : true
                }
            });
            if(tinfo && tinfo.member){
                (ctx as any).member = tinfo.member;
                return true;
            }
        }
        ctx.res.send(JSON.stringify(failed("not login"))) 
        return false;
    }
});



